#ifndef READ_CONFIG_H
#define READ_CONFIG_H





class config_file
{
public:
typedef map<string, string, less<string>, allocator<string> > VALUE_PAIR;
	config_file(string file)
		: filename(file)
	{
		instance = this;
	}
	string trim(string str)
	{
		string s;
		// Trim Both leading and trailing spaces
		size_t startpos = str.find_first_not_of(" \t"); // Find the first character position after excluding leading blank spaces
		size_t endpos = str.find_last_not_of(" \t"); // Find the first character position from reverse af

		// if all spaces or empty return an empty string
		if(( string::npos == startpos ) || ( string::npos == endpos))
		{
			s = "";
		}
		else
			s = str.substr( startpos, endpos-startpos+1 );
		return s;
	}
	int is_comment(string line)
	{
		// blank lines are comments
		if(line.size() == 0) return 1;

		// lines starting with # are comments
		if(trim(line)[0] == '#') return 1;

		// lines with only whitespace are comments
		for(unsigned int i = 0; i < line.size(); i++)
			if(!isspace(line[i]))
				return 0;
		return 1;
	}
	string attribute(string line)
	{
		return trim(line.substr(0, line.find_first_of('=')));
	}

	string value(string line)
	{
		return trim(line.substr(line.find_first_of('=')+1));
	}

	int read_config_file()
	{
		ifstream the_file(filename.c_str());

		if(!the_file.is_open())
		{
			cerr << "couldn't read config file.\n";
			return 0;
		}

		string name = "", line;

		while (!the_file.eof())
		{
			getline(the_file, line);

			if(is_comment(line)) continue;

			if ((line.find_first_of('=') == string::npos) || (attribute(line) == "") || (value(line) == ""))
			{
				cerr << "Warning: ignored bad line in pattern file:\n\t" << line <<endl;
				continue;
			}
			config_vp[attribute(line)] = value(line);

		}
		return 1;
	}
	bool contain(string at)
	{
		return config_vp.find(at) != config_vp.end();
	}
	string &get_value(string at)
	{
		return config_vp[at];
	}
	bool value_yes(string at)
	{
		return config_vp.find(at) != config_vp.end() && config_vp[at] == "YES";
	}
	const char *get_config(char *value)
	{
		VALUE_PAIR::iterator theIterator;
		theIterator = config_vp.find(value);
		if(theIterator != config_vp.end())
			return (*theIterator).second.c_str();
		else    // some character other than 0 - 9
			return NULL;
	}
	VALUE_PAIR config_vp;
	string filename;

	static class config_file *instance;
};






















#endif